package com.starsoft.mathlib.macd;

import java.util.List;

import com.tictactec.ta.lib.Compatibility;
import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;

public class MacdCaculator {

	private static final int DEFAULT_FAST_PERIOD = 12;
	private static final int DEFAULT_SLOW_PERIOD = 26;
	private static final int DEFAULT_SIGNAL_PERIOD = 9;
	
	public static int getPriceNumber(int number) {
		return DEFAULT_SLOW_PERIOD + DEFAULT_SIGNAL_PERIOD + number;
	}

	public static MacdReslult caculate(List<Double> prices, int retCount) {
		double[] pricesArray = new double[prices.size()];
		for (int i = 0; i < prices.size(); i++) {
			pricesArray[i] = prices.get(i);
		}
		return caculate(pricesArray, retCount);
	}

	public static MacdReslult caculate(double[] prices, int retCount) {
		int startIndex = prices.length - retCount;
		double[] inReal = prices;
		double[] outMACD = new double[retCount];
		double[] outMACDSignal = new double[retCount];
		double[] outMACDHist = new double[retCount];
		MInteger outBegIdx = new MInteger();
		MInteger outNbElement = new MInteger();

		Core talib = new Core();
		talib.SetCompatibility(Compatibility.Metastock);
		RetCode retCode = talib.macd(startIndex, inReal.length - 1, inReal, DEFAULT_FAST_PERIOD, DEFAULT_SLOW_PERIOD,
				DEFAULT_SIGNAL_PERIOD, outBegIdx, outNbElement, outMACD, outMACDSignal, outMACDHist);

		MacdReslult result = new MacdReslult(retCode, outNbElement.value, outMACD, outMACDSignal, outMACDHist);
		return result;
	}

}
